Completed
Push — master ( 0a8aee...45be8d )
by Andres
30s
created

angular.service(ꞌutilꞌ)   B

Complexity

Conditions 1
Paths 64

Size

Total Lines 93

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
c 3
b 0
f 0
nc 64
dl 0
loc 93
rs 8.4642
nop 2

10 Functions

Rating   Name   Duplication   Size   Complexity  
A ��) 0 7 2
A ��) 0 3 1
B ��) 0 7 6
B ��) 0 17 5
A ��) 0 9 3
A ��) 0 8 3
A ��) 0 10 4
A ��) 0 3 1
A ��) 0 5 1
A ��) 0 7 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/* globals numberformat,window */
2
/**
3
 util
4
 Utility service with misc. functions.
5
6
 @namespace Services
7
 */
8
'use strict';
9
10
angular
11
  .module('game')
12
  .service('util', ['$sce',
13
    'data',
14
    function($sce, data) {
15
      let sv = this;
16
      /* Return the HTML representation of an element, or the element itself
17
      if it doesn't have one */
18
      sv.getHTML = function(resource) {
19
        let html = data.html[resource];
20
        if (typeof html === 'undefined' && data.resources[resource]) {
21
          html = data.resources[resource].html;
22
        }
23
        if (typeof html === 'undefined') {
24
          return resource;
25
        }
26
        return html;
27
      };
28
29
      sv.prettifyNumber = function(number, player) {
30
        if (typeof number === 'undefined' || number === null) return null;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
31
        if (number === '') return '';
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
32
        if (number === Infinity) return '∞';
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
33
        if (number === 0) return '0';
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
34
        return numberformat.format(number, player.options.numberformat);
35
      };
36
37
      sv.addResource = function(player, scope, key, quantity, state){
38
        player.resources[key].number += quantity;
39
        sv.addStatistic(player, scope, key, quantity);
40
        if (quantity > 0 && !player.resources[key].unlocked) {
41
          player.resources[key].unlocked = true;
42
          state.addNew(key);
43
        }
44
      };
45
46
      /* Adds an statistic. Scope can be only all time, dark run,
47
        only for specific elements, or for all elements at once
48
      */
49
      sv.addStatistic = function(player, scope, key, value){
50
        setStatistic(player.statistics.all_time, key, value);
51
        if(scope === 'all_time'){
52
          return;
53
        }
54
        setStatistic(player.statistics.dark_run, key, value);
55
        if(scope === 'dark'){
56
          return;
57
        }
58
        if(scope === 'all_elements') {
59
          scope = Object.keys(data.elements);
60
        }
61
        for(let element of scope){
62
          player.statistics.exotic_run[element] = player.statistics.exotic_run[element] || {};
63
          setStatistic(player.statistics.exotic_run[element], key, value);
64
        }
65
      };
66
67
      function setStatistic(bucket, key, value){
68
        // If it is numeric, add it up
69
        if(!isNaN(parseFloat(value)) && isFinite(value)){
70
          bucket[key] = bucket[key]+value || value;
71
        // otherwise, replace
72
        }else{
73
          bucket[key] = value;
74
        }
75
      }
76
77
      sv.trustHTML = function(html) {
78
        return $sce.trustAsHtml(html);
79
      };
80
81
      sv.nextAmount = function (player, index, array) {
82
        player.options[index] = (player.options[index] + 1) % array.length;
83
      };
84
85
      sv.delayedExec = function(currentTs, eventTs, delay, callback) {
86
        if(currentTs-eventTs >= delay){
87
          callback();
88
        }else{
89
          window.requestAnimationFrame((ts) => sv.delayedExec(ts, eventTs, delay, callback));
90
        }
91
      };
92
93
      sv.prestigeProduction = function(number, start, power){
94
        number = number || 0;
95
        let production = Math.pow(Math.E,(-0.5+Math.sqrt(0.25+0.8686*Math.log(number/start)))/(2/Math.log(power*power))) || 0;
96
        return Math.round(Math.max(0, production));
97
      };
98
99
      sv.calculateValue = function(number, value, level){
100
        let result = number;
101
        if(value.linear) result *= level*value.linear;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
102
        if(value.poly) result *= Math.floor(Math.pow(level, value.poly));
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
103
        if(value.exp) result *= Math.floor(Math.pow(value.exp, level));
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
104
        return result;
105
      }
106
    }
107
  ]);
108